home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / B2A.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  5KB  |  199 lines

  1. /* b2a.c via btoa: version 4.0
  2.  * stream filter to change 8 bit bytes into printable ascii
  3.  * computes the number of bytes, and three kinds of simple checksums
  4.  * incoming bytes are collected into 32-bit words, then printed in base 85
  5.  * exp(85,5) > exp(256,4) == exp(2,32) 
  6.  * the ASCII characters used are between '!' and 'u'
  7.  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
  8.  *
  9.  *  Paul Rutter     Joe Orost      DosPort WildHack
  10.  *  philabs!per     petsd!joe      Severely!Mangled
  11.  *
  12.  *  WARNING: this version is not compatible with the original as sent out
  13.  *  on the net.  The original encoded from ' ' to 't'; which cause problems
  14.  *  with some mailers (stripping off trailing blanks).
  15.  */
  16. #include <stdio.h>
  17. #include "ext_refs.c"
  18.  
  19. #define reg register
  20. #define MAXPERLINE 78
  21.  
  22. void encode(reg c);
  23. void wordout(reg long int word);
  24. void charout(int c);
  25.  
  26. long int Ceor = 0;
  27. long int Csum = 0;
  28. long int Crot = 0;
  29. long int ccount = 0;
  30. long int bcount = 0;
  31. long int word;
  32.  
  33. #define EN(c)   (int) ((c) + '!')
  34.  
  35. void encode(reg c)
  36. {
  37.   Ceor ^= c;
  38.   Csum += c;
  39.   Csum += 1;
  40.   if ((Crot & 0x80000000L)) {
  41.     Crot <<= 1;
  42.     Crot += 1;
  43.   } else {
  44.     Crot <<= 1;
  45.   }
  46.   Crot += c;
  47.  
  48.   word <<= 8;                   /* Pack 4 characters into 32 bit word */
  49.   word |= c;
  50.   if (bcount == 3) {
  51.     wordout(word);
  52.     bcount = 0;
  53.   } else {
  54.     bcount += 1;
  55.   }
  56. }
  57.  
  58. void wordout(reg long int word)
  59. {
  60.   if (word == 0) {
  61.     charout('z');
  62.   } else {
  63.     reg int tmp = 0;
  64.  
  65.     if(word < 0) {  /* Because some don't support unsigned long */
  66.       tmp = 32;
  67.       word = word - ((long)85 * 85 * 85 * 85 * 32);
  68.     }
  69.     if(word < 0) {
  70.       tmp = 64;
  71.       word = word - ((long)85 * 85 * 85 * 85 * 32);
  72.     }
  73.     charout(EN((word / ((long)85 * 85 * 85 * 85)) + tmp));
  74.     word %= ((long)85 * 85 * 85 * 85);
  75.     charout(EN(word / ((long)85 * 85 * 85)));
  76.     word %= ((long)85 * 85 * 85);
  77.     charout(EN(word / (85 * 85)));
  78.     word %= (85 * 85);
  79.     charout(EN(word / 85));
  80.     word %= 85;
  81.     charout(EN(word));
  82.   }
  83. }
  84.  
  85. void charout(int c)
  86. {
  87.   putchar(c);
  88.   ccount += 1;
  89.   if (ccount == MAXPERLINE) {
  90.     putchar('\n');
  91.     ccount = 0;
  92.   }
  93. }
  94.  
  95. main(int argc, char *argv[])
  96. {
  97.   reg c;
  98.   reg long int n;
  99.   int fs_h_si, fs_h_so;
  100.  
  101. #if 0
  102.   if (argc != 1) {
  103.     error(2, __FILE__, __LINE__, "bad args to %s\n", argv[0]);
  104.   }
  105. #else
  106.   /* Filter function suppressed until binary I/O is provided */
  107.   switch (argc) {
  108.   default:
  109.     error(1, __FILE__, __LINE__, "bad args to %s\n"
  110.     "Usage:\nB2A  InFile.Bin  [OutFile.B2A]\n"
  111.     "B2A        : converts Binary images to printing ASCII characters\n"
  112.     "InFile.Bin : name of the existing, binary format, file to convert\n"
  113.     "OutFile.B2A: name for the file to get the ASCII encoded format\n"
  114.     , argv[0]);
  115.     break;
  116.   case 3:
  117.     fs_h_so=fs_chng(stdout, argv[2], "w"   );
  118.   /*break;*/
  119.   case 2:
  120.     fs_h_si=fs_chng(stdin , argv[1], OPN_RD);
  121.     if (fs_h_si < 0 || fs_h_so < 0) 
  122.         error(3, __FILE__, __LINE__, "bad args to %s\n", argv[0]);
  123.     break;
  124.   }
  125. #endif
  126.   printf("xbtoa Begin\n");
  127.   n = 0;
  128.   while ((c = getchar()) != EOF) {
  129.     encode(c);
  130.     n += 1;
  131.   }
  132.   while (bcount != 0) {
  133.     encode(0);
  134.   }
  135.   /* n is written twice as crude cross check*/
  136.   printf("\nxbtoa End N %ld %lx E %lx S %lx R %lx\n", n, n, Ceor, Csum, Crot);
  137.   switch (argc) {
  138.   case 3:
  139.     fs_h_so=fs_rest( stdout, fs_h_so);
  140.   /*break;*/
  141.   case 2:
  142.     fs_h_si=fs_rest( stdin , fs_h_si);
  143.     if (fs_h_si < 0 || fs_h_so < 0)
  144.         error(4, __FILE__, __LINE__, "bad args to %s\n", argv[0]);
  145.     break;
  146.   default: break;
  147.   }
  148.   exit(0);
  149. return 0; /* Silence the warning without void'ing the function */
  150. }
  151.  
  152. /* ext_refs.c
  153.  * debugging, provide prototypes, single error exit,
  154.  * redirect the first two arguments in place of stdin/out,
  155.  * open files in binary mode (changed to OPN_??? to reflect that)
  156.  * SEEKING: a method to turn this back into a filter, (binary stdin/out)
  157.  *                                 DosPort WildHack
  158.  *                                 Severely!Mangled
  159.  */
  160. #include <stdlib.h>
  161. #include <string.h>
  162. #include <io.h>
  163.  
  164. #define OPN_RD "rb"
  165. #define OPN_WR "wb"
  166. #define OPN_W_R "w+b"
  167. #if 0
  168. extern unsigned _stklen  = 1024 * 4;
  169. extern unsigned _heaplen = 1024 * 0;
  170. #endif
  171. int error(int code, char *file, int line, char *msg,...);
  172. int error(int code, char *file, int line, char *msg,...)
  173. {
  174.     va_list argptr;
  175.     fprintf(stderr,"\n?Error(%d) %s # %d - ",code,file,line);
  176.     va_start(argptr, msg);
  177.     vfprintf(stderr, msg, argptr);
  178.     va_end(argptr);
  179.     fputc('\n',stderr);
  180.     exit(code);
  181. return 0;
  182. }
  183. int fs_chng(FILE *ChgStr, char *NewPath, char *OpenMode);
  184. int fs_chng(FILE *ChgStr, char *NewPath, char *OpenMode)
  185. {
  186.     int   DupHndl=dup(fileno(ChgStr));    /* Save current handle info    */
  187.     freopen(NewPath, OpenMode, ChgStr);   /* Redirect stream to new file */
  188.     return DupHndl;                       
  189. }
  190. int fs_rest(FILE *RstStr, int DupHndl);
  191. int fs_rest(FILE *RstStr, int DupHndl)
  192. {
  193.     int Err = fileno(RstStr);           /* Save the handle across fclose */
  194.     fflush(RstStr); /*fclose(RstStr);*/ /* Close the redefined stream &  */
  195.     Err=dup2(DupHndl,Err);              /* Restore the prior stream info */
  196.     close(DupHndl);                     /* Discard the saved file handle */
  197.     return Err;
  198. }
  199.